import 'dart:math';

import 'package:casdon/modules/recipe/domain/states/tab_state.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

// 状态控制器
class TabControllerNotifier extends StateNotifier<TabState> {
  TabControllerNotifier(List<String> tabs) : super(TabState(tabs: tabs));

  // 添加标签
  void addTab(String title) {
    state = state.copyWith(tabs: [...state.tabs, title]);
  }

  // 删除标签（保留至少一个）
  void removeTab(int index) {
    if (state.tabs.length > 1) {
      final newTabs = List<String>.from(state.tabs)..removeAt(index);
      final newIndex = min(state.currentIndex, newTabs.length - 1);
      state = state.copyWith(
        tabs: newTabs,
        currentIndex: newIndex,
      );
    }
  }

  // 切换标签
  void setIndex(int index) {
    state = state.copyWith(currentIndex: index);
  }
}

